package com.pankajm.gre;

import java.util.StringTokenizer;

public class WordItem {

	private static final String SEPERATOR = "-->";
	private String word;
	private String meaning;
	
	public void setMeaning(String meaning) {
		this.meaning = meaning;
	}
	
	public String getMeaning() {
		return meaning;
	}
	
	public void setWord(String word) {
		this.word = word;
	}
	
	public String getWord() {
		return word;
	}
	
	/**
	 * A word meaning string is word-->meaning
	 */
	public static WordItem fromString(String wordMeaningString) {
		WordItem wordItem = new WordItem();
		int seperatorPos = wordMeaningString.indexOf(SEPERATOR);
		wordItem.setWord(wordMeaningString.substring(0, seperatorPos));
		wordItem.setMeaning(wordMeaningString.substring(
			seperatorPos + SEPERATOR.length()));
		filterMeaning(wordItem);
		return wordItem;
	}

	private static void filterMeaning(WordItem wordItem) {
		StringTokenizer tokenizer = new StringTokenizer(wordItem.getMeaning(), ";");
		StringBuilder builder = new StringBuilder();
		
		while (tokenizer.hasMoreTokens()) {
			String token = tokenizer.nextToken().trim();
			if (!token.contains(wordItem.getWord())) {
				builder.append(token).append("; ");
			}
		}
		wordItem.setMeaning(builder.toString().trim());
	}
}
